package com.scu.dao;

import com.scu.entity.LikeCount;
import com.scu.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LikeDao {

    //查询点赞
    public static boolean whethereLike(LikeCount likeCount) throws SQLException {
        Connection conn = JDBCUtil.getConnection();

        String sql = "select * from likecount where post_id = 5 and user_id = 194432";
        PreparedStatement prep = conn.prepareStatement(sql);

        ResultSet rs = prep.executeQuery();

        boolean like = rs.next();
        //4. 关闭连接
        conn.close();

        return like;
    }

    //点赞
    public static int like(LikeCount likeCount) throws SQLException {
        //1. 连接
        Connection conn = JDBCUtil.getConnection();


        //2. sql

        String sql = "INSERT INTO likecount(post_id,user_id) VALUES(?,?)";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setInt(1,likeCount.getPost_id());
        prep.setInt(2, likeCount.getUser_id());
            //prep.setString(2, post.getPost_content());


        //3. 结果
        int row = prep.executeUpdate();

        //4. 关闭连接
        conn.close();

        return row;
    }

    public static int deleteLike(LikeCount likeCount) throws SQLException {
        Connection conn = JDBCUtil.getConnection();

        String sql = "delete from likecount where post_id=? and user_id = ?";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setInt(1,likeCount.getPost_id());
        prep.setInt(2,likeCount.getUser_id());

        int row = prep.executeUpdate();

        System.out.println("删除成功"+likeCount.getPost_id());

        conn.close();

        return row;
    }

//    public List<LikeCount> findUser() throws SQLException {
//
//        Connection conn = JDBCUtil.getConnection();
//
//        String sql = "select * from likecount where post_id = 1277365";
//        PreparedStatement prep = conn.prepareStatement(sql);
//
//        ResultSet rs = prep.executeQuery();
//
//        List<LikeCount> list = new ArrayList<LikeCount>();
//        while(rs.next()){
//            //rs代表的就是每一行
//            LikeCount likeCount = new LikeCount();
//
//            int post_id = rs.getInt("post_id");
//            //String post_content = rs.getString("post_content");
//            int user_id = rs.getInt("user_id");
//
//            likeCount.setPost_id(post_id);
//            //post.setPost_content(post_content);
//            likeCount.setLike_id(user_id);
//
//            list.add(likeCount);
//        }
//
//        //4. 关闭连接
//        conn.close();
//
//        return list;
//    }

    public static void main(String[] args) throws SQLException {
        LikeCount likeCount = new LikeCount();
        likeCount.setPost_id(5);
        likeCount.setUser_id(194432);
        //int insert = LikeDao.like(likeCount);
        boolean like = LikeDao.whethereLike(likeCount);
        if(like)
        {
            int delete = LikeDao.deleteLike(likeCount);
        }
        else{
            int insert = LikeDao.like(likeCount);
        }

    }
}
